﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace ClipPlayer
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
    }
    //====================================================================================================================
    public static class WmeUtils
    {
        private static System.Windows.Threading.DispatcherTimer dtProgress = new System.Windows.Threading.DispatcherTimer() { Interval = new TimeSpan(0, 0, 0, 0, 450) };
        private static ProgressBar pb = null;
        private static MediaElement me = null;
        //------------------------------------------------------------------
        public static bool GeneralPlay(this MediaElement wme, ProgressBar pbSrc)
        {
            if (null == pbSrc) return false;
            pb = pbSrc;
            me = wme;
            if (null != pb) pb.Value = pb.Minimum;
            dtProgress.Tag = (object)pb;
            dtProgress.Tick += DtProgress_Tick;
            dtProgress.Start();
            wme.Play();
            return true;
        }
        //------------------------------------------------------------------
        public static bool GeneralStop(this MediaElement wme)
        {
            dtProgress.Stop();
            if (null != pb) pb.Value = pb.Maximum;
            //
            dtProgress.Tick -= DtProgress_Tick;
            //
            if (null != pb) pb.Value = pb.Minimum;
            wme.Stop();
            wme.Close();
            return true;
        }
        //------------------------------------------------------------------
        private static void DtProgress_Tick(object sender, EventArgs e)
        {
            ProgressBar pb0 = dtProgress.Tag as ProgressBar;
            //pb0.Value = (int)(me.CurrentProgress() * (pb0.Maximum - pb0.Minimum));
            //System.Windows.Threading.Dispatcher.Invoke(new Action(() => pb.Value = (int)(wme.Position.input.Position * 100.0)));
            if (me.NaturalDuration.HasTimeSpan)
            {
                if (me.Position.Ticks == me.NaturalDuration.TimeSpan.Ticks)
                {
                    //wmp.NaturalDuration.HasTimeSpan false bez Play
                    if (null != pb0) pb0.Value = pb0.Maximum;
                    dtProgress.Stop();
                    me.Close();
                    if (null != pb0) pb0.Value = pb0.Minimum;
                    //
                    // End of Clip
                    //
                }
                else
                {
                    if (null != pb0)
                    {
                        double currentProgress = (pb0.Maximum - pb0.Minimum) * (((double)me.Position.TotalSeconds / me.NaturalDuration.TimeSpan.TotalSeconds));
                        if (currentProgress > pb0.Value)
                        {
                            pb0.Value = currentProgress;
                            System.Threading.Thread.Sleep(1);
                            //DoEvents();
                        }
                    }
                    //Dispatcher.Invoke(new Action(() => progressBar1.Value = progressBar1.Maximum*j));
                }
            }
        }
        //------------------------------------------------------------------
        //------------------------------------------------------------------
        public static bool IsPlayable(this MediaElement wme)
        {
            return wme.HasAudio && wme.HasVideo;
        }
        //------------------------------------------------------------------
        public static bool IsPlaying(this MediaElement wme)
        {
            return wme.NaturalDuration.HasTimeSpan && (wme.Position.Ticks > 0);
        }
        //------------------------------------------------------------------
        public static double CurrentProgress(this MediaElement wme)
        {
            return (double)wme.Position.TotalSeconds / wme.NaturalDuration.TimeSpan.TotalSeconds;
        }
        //------------------------------------------------------------------
        public static double SetProgress(this MediaElement wme, double decimalPartValue)
        {
            wme.Position = new TimeSpan(0, 0, (int)(wme.NaturalDuration.TimeSpan.TotalSeconds * decimalPartValue));
            //wme.Position = new TimeSpan((long)(wme.NaturalDuration.TimeSpan.Ticks * decimalPartValue));
            return (double)wme.Position.Seconds;
        }
        //====================================================================================================================
    }//class
}//namespace
